<template>
  <el-container>
    <el-header>
      <div class="left-panel">
        <el-button v-if="$AUTH('purchase:purchaseOrder:add')" type="primary" icon="el-icon-plus" @click="add"></el-button>
      </div>
      <div class="right-panel">
        <div class="right-panel-search">
          <!-- 日期时间范围 -->
          <el-date-picker v-model="search.createTime" type="datetimerange" value-format="YYYY-MM-DD HH:mm:ss" start-placeholder="开始日期" end-placeholder="结束日期" style="width: 100%;"></el-date-picker>
          <el-input v-model="search.purchaseOrderId" placeholder="采购单" clearable></el-input>
          <el-input v-model="search.departmentId" placeholder="企业" clearable></el-input>
          <el-input v-model="search.supplierId" placeholder="供应商" clearable></el-input>
          <el-input v-model="search.sysUserId" placeholder="跟单人" clearable></el-input>
          <el-input v-model="search.orderNo" placeholder="订单号" clearable></el-input>
          <el-input v-model="search.payable" placeholder="应付款" clearable></el-input>
          <el-input v-model="search.discount" placeholder="折后付款" clearable></el-input>
          <el-input v-model="search.downPayment" placeholder="预付定金" clearable></el-input>
          <el-input v-model="search.msg" placeholder="备注" clearable></el-input>
          <el-input v-model="search.createId" placeholder="制单人" clearable></el-input>
          <el-input v-model="search.createTime" placeholder="创建时间" clearable></el-input>
          <el-input v-model="search.flowStatus" placeholder="流程状态" clearable></el-input>
          <el-input v-model="search.deleted" placeholder="" clearable></el-input>
          <el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
        </div>
      </div>
    </el-header>
    <el-main class="nopadding">
      <scTable ref="table" :apiObj="apiObj" row-key="purchaseOrderId" @selection-change="selectionChange" stripe>
        <el-table-column type="selection" width="50"></el-table-column>
        <el-table-column label="采购单" prop="purchaseOrderId" width="150"></el-table-column>
        <el-table-column label="企业" prop="departmentId" width="150"></el-table-column>
        <el-table-column label="供应商" prop="supplierId" width="150"></el-table-column>
        <el-table-column label="跟单人" prop="sysUserId" width="150"></el-table-column>
        <el-table-column label="订单号" prop="orderNo" width="150"></el-table-column>
        <el-table-column label="应付款" prop="payable" width="150"></el-table-column>
        <el-table-column label="折后付款" prop="discount" width="150"></el-table-column>
        <el-table-column label="预付定金" prop="downPayment" width="150"></el-table-column>
        <el-table-column label="备注" prop="msg" width="150"></el-table-column>
        <el-table-column label="制单人" prop="createId" width="150"></el-table-column>
        <el-table-column label="创建时间" prop="createTime" width="150"></el-table-column>
        <el-table-column label="流程状态" prop="flowStatus" width="150"></el-table-column>
        <el-table-column label="" prop="deleted" width="150"></el-table-column>
<!--    <el-table-column label="创建时间" prop="createTime" width="150" :formatter="formatDate"></el-table-column>-->
        <el-table-column label="操作" fixed="right" align="right">
          <template #default="scope">
            <el-button v-if="$AUTH('purchase:purchaseOrder:edit')" type="text" size="small" @click="table_edit(scope.row, scope.$index)">编辑</el-button>
            <el-divider direction="vertical"></el-divider>
            <el-button v-if="$AUTH('purchase:purchaseOrder:del')" type="text" size="small" @click="table_del(scope.row, scope.$index)">删除</el-button>
          </template>
        </el-table-column>
      </scTable>
    </el-main>
  </el-container>

  <purchaseOrderEdit-dialog v-if="dialog.purchaseOrderEdit" ref="purchaseOrderEditDialog" @success="handleSaveSuccess" @closed="dialog.purchaseOrderEdit=false"></purchaseOrderEdit-dialog>
</template>

<script>
  import purchaseAPI from '@/api/purchase/purchaseOrder';
  import purchaseOrderEditDialog from './purchaseOrderEdit';//1--导入
  export default {
    name: 'purchaseOrder',
    components: {
        purchaseOrderEditDialog//2发布
    },
    data() {
      return {
        dialog: {
          purchaseOrderEdit: false
        },
        apiObj: purchaseAPI.purchaseOrder.list,
        selection: [],
        search: {
          keyword: null,
          createTime: null
        }
      }
    },
    methods: {
      //添加
      add(){
        this.dialog.purchaseOrderEdit = true
        this.$nextTick(() => {
          this.$refs.purchaseOrderEditDialog.open()
        })
      },
      //编辑
      table_edit(row){
        this.dialog.purchaseOrderEdit = true
        this.$nextTick(() => {
          this.$refs.purchaseOrderEditDialog.open('edit').setData(row)
        })
      },
      //删除
      async table_del(row){
        var confirm = await this.$confirm('确定删除选中的项吗？', '提示', {
          confirmButtonText: 'ok',
          type: 'warning'
        }).catch(() => {})
        if(confirm !== 'confirm'){
          return false;
        }
        var res = await purchaseAPI.purchaseOrder.delete.delete(row.purchaseOrderId);
        if(res.code === 2000){
          this.$refs.table.refresh();
          this.$message.success("删除成功");
        }else{
          this.$alert(res.message, "提示", {type: 'error'});
        }
      },
      //表格选择后回调事件
      selectionChange(selection){
        this.selection = selection;
      },
      //搜索
      upsearch(){
        this.$refs.table.upData(this.search);
      },
      //本地更新数据
      handleSaveSuccess(){
        this.$refs.table.refresh();
      },
      handlePermissionSuccess(){
        this.$refs.table.refresh();
      },
      handleDataSuccess(){
        this.$refs.table.refresh();
      },
      formatDate(row,column) {// 获取单元格数据
        let data = row[column.property];
        if(data == null) {
          return null;
        }
        let dt = new Date(data);
        return dt.getFullYear() + '-' + (dt.getMonth() + 1) + '-' + dt.getDate() + ' ' + dt.getHours() + ':' + dt.getMinutes() + ':' + dt.getSeconds();
      },
    }
  }
</script>
<style>
</style>
